#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
using ll=long long;
const int M=1e4+10;


int n,m;
int f[510];
ll dist[510][510];
struct node {
	int x,y;
	ll z;
	bool flg;
	bool operator<(const struct node& p)
	{
		return z<p.z;
	}
};
vector<node> edge;
vector<pair<int,ll>> edged[510];
int Find(int x)
{
	if(f[x]!=x)
		f[x]=Find(f[x]);
	return f[x];
}
void dfs(int u,int fa,ll maxd,ll dist[])
{
	dist[u]=maxd;
	for(auto& e:edged[u])
	{
		if(e.first!=fa)
		{
			dfs(e.first,u,max(maxd,e.second),dist);			
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int a,b;
		ll c;
		cin>>a>>b>>c;
		edge.push_back({a,b,c,false});	
		
	}
	
	sort(edge.begin(),edge.end());
	for(int i=0;i<=n;i++)
		f[i]=i;
	ll sum=0;
	for(int i=0;i<edge.size();i++)
	{
		int a=edge[i].x,b=edge[i].y;
		ll c=edge[i].z;
		a=Find(a),b=Find(b);
		if(a!=b)
		{
			edge[i].flg=true;
			f[a]=b;
			sum+=c;
			edged[a].push_back({b,c});
			edged[b].push_back({a,c});
		}
	}
	
	for(int i=1;i<=n;i++)
	{
		dfs(i,-1,0,dist[i]);
	}
	ll ret=1e18;
	for(int i=0;i<edge.size();i++)
	{
		if(edge[i].flg)
			continue;
		int a=edge[i].x,b=edge[i].y;
		if(edge[i].z>dist[a][b])
			ret=min(ret,sum+edge[i].z-dist[a][b]);	
	}
	cout<<ret<<endl;
	return 0;
}
